<!-- 信息填报 -->
<template>
	<view class="setting-container" :style="{ height: `${windowHeight}px` }">
		<view class="menu-list">
			<view class="customFormClass">
				<u--form labelPosition="top" :model="submitParams" :rules="rules" ref="uForm" labelWidth="auto"
					:labelStyle="{'fontWeight': '600'}">
					<!-- 日期 -->
					<u-form-item label="日期" prop="submitParams.date"  @click="showDate = true; hideKeyboard()"
						ref="item1">
						<u--input v-model="submitParams.date" disabled disabledColor="#ffffff" placeholder="请选择日期"
							></u--input>
						<!-- <u-icon slot="right" name="arrow-right"></u-icon> -->
					</u-form-item>

					<u-form-item v-for="item in fieldData" :key="item.id" :label="item.chineseName" :prop="item.englishName"
						>
						<u--textarea v-if="item.fieldType == 2" v-model="submitParams[item.englishName]"
							:placeholder="item.defaultValue ? item.defaultValue : ''" ></u--textarea>
						<u--input v-if="item.fieldType == 1 || item.fieldType == 3" v-model="submitParams[item.englishName]"
							:type="item.fieldType == 1 ? 'text' : item.fieldType == 3 ? 'number' : 'text'" 
							:placeholder="item.defaultValue ? item.defaultValue : ''"></u--input>
					</u-form-item>
				</u--form>
			</view>
			<u-gap height="10"></u-gap>
			<u-button type="primary" text="提交" @click="submit"></u-button>
		</view>

		<u-datetime-picker :show="showDate" v-model="todayNewDate" mode="date"
			@confirm='dateConfirm' @cancel="showDate = false"></u-datetime-picker>
	</view>
</template>
<script>
	import {
		queryFieldConfigByUser,
		addOrchardDataWrite
	} from '@/api/infoReporting';
	export default {
		data() {
			return {
				windowHeight: uni.getSystemInfoSync().windowHeight,
				submitParams: {
					landId: '',
				},
				fieldData: [],
				rules: {
					"xxxxx": {
						type: 'string',
						required: true,
						message: "请填写内容",
						trigger: ["blur", "change"],
					}
				},
				todayNewDate: uni.$u.timeFormat(Number(new Date()), 'yyyy-mm-dd'),
				showDate: false
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		created() {
			this.initForm();
			this.submitParams.landId = this.$store.getters.landData.id;
		},
		methods: {
			async initForm() {
				//查询字段权限
				let {
					data
				} = await queryFieldConfigByUser({
					userId: this.$store.getters.userId
				});
				this.fieldData = data;
				//封装数据
				data.map(item => {
					//需要保存的字段
					this.$set(this.submitParams, item.englishName, '');
					//必填项
					if (item.isRequired == 1) {
						let rule = {
							type: item.fieldType == 3 ? 'number' : 'string',
							required: true,
							message: "请填写内容",
							trigger: ["blur", "change"],
						}
						this.$set(this.rules, item.englishName, rule)
					}
					//默认值
					// if(item.defaultValue) {
					// 	this.$set(this.submitParams, item.englishName, item.defaultValue);
					// }
				})
				this.submitParams.date = uni.$u.timeFormat(Number(new Date()), 'yyyy-mm-dd');
				this.$nextTick(() => {
					this.$refs.uForm.setRules(this.rules);
				});
			},
			submit() {
				this.$refs.uForm.validate().then(async (response) => {
					await addOrchardDataWrite(this.submitParams);
					this.$modal.msgSuccess("提交成功")
					setTimeout(() => {
						this.$tab.navigateBack()
					}, 1500);
				}).catch(errors => {

				})
			},
			dateConfirm(data) {
				console.log(data)
				this.submitParams.date = uni.$u.timeFormat(data.value, 'yyyy-mm-dd');
				this.showDate = false;
			}
		}
	};
</script>
<style lang="scss" scoped>
	.menu-list {
		margin: 8px !important;
	}

	.customFormClass {
		background: #fff;
		border-radius: 10rpx;
		padding: 22rpx;
	}
</style>